package com.fr.plugin.utils;

import com.fr.stable.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.regex.Matcher;

/***
 * @desc:
 * @author: zhuosh
 * @date: 2022-05-06
 */
public class IpUtil {

    private static final String UNKNOWN = "unknown";
    private static final String LOCALHOST_IP = "127.0.0.1";
    // 客户端与服务器同为一台机器，获取的 ip 有时候是 ipv6 格式
    private static final String LOCALHOST_IPV6 = "0:0:0:0:0:0:0:1";
    private static final String SEPARATOR = ",";


    private static String reg = "1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d";


    // 根据 HttpServletRequest 获取 IP
    public static String getIpAddress(HttpServletRequest request) {
        if (request == null) {
            return "unknown";
        }
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if (LOCALHOST_IP.equalsIgnoreCase(ip) || LOCALHOST_IPV6.equalsIgnoreCase(ip)) {
                // 根据网卡取本机配置的 IP
                InetAddress iNet = null;
                try {
                    iNet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                if (iNet != null)
                    ip = iNet.getHostAddress();
            }
        }
        // 对于通过多个代理的情况，分割出第一个 IP
        if (ip != null && ip.length() > 15) {
            if (ip.indexOf(SEPARATOR) > 0) {
                ip = ip.substring(0, ip.indexOf(SEPARATOR));
            }
        }
        return LOCALHOST_IPV6.equals(ip) ? LOCALHOST_IP : ip;
    }


    /***
     * ip 规格校验
     * 192.168.1.1
     * *.*.*.*
     * 192.168.1-100.100
     * @param ip
     * @return
     */
    public static boolean ipValid(String ip){
        if(StringUtils.isEmpty(ip)){
            return false;
        }
        String[] arrs = ip.split("\\.");
        if(arrs.length != 4){
            return false;
        }
        for(int i=0; i<arrs.length; i++) {
            if("*".equals(arrs[i])){
                continue;
            } else if(arrs[i].contains("-")){
                String[] subNum = arrs[i].split("-");
                if(subNum.length != 2){
                    return false;
                }
                if(!numMatch(subNum[0]) || !numMatch(subNum[1])){
                    return false;
                }
                if(Integer.parseInt(subNum[0]) >= Integer.parseInt(subNum[1])){
                    return false;
                }
            }else if(!numMatch(arrs[i])){
                return false;
            }
        }
        return true;
    }



    private static boolean numMatch(String num){
        return num.matches(reg);
    }


    public static void main(String[] args){
        System.out.println(ipValid("255.255.*.100-10"));
    }
}
